﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BL;

namespace ShippingID.Controllers
{
    public class MotoristaController : Controller
    {
        //
        // GET: /Motorista/

        public ActionResult Index()
        {
            addview();
            IList<Modelo.Motorista> mot = new ServiceGeral().RetornarTodos<Modelo.Motorista>();
            foreach (Modelo.Motorista item in mot)
            {
                if(item.IdTransportadora!=null)
                item.Transportadora = ((List<Modelo.Transportadora>)ViewData["Transportadora"]).SingleOrDefault(T => T.Id == item.IdTransportadora);
            }
            return View(mot);
        }

        //
        // GET: /Motorista/Details/5

        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /Motorista/Create

        public ActionResult Create()
        {
            addview();
            return View();
        } 

        //
        // POST: /Motorista/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            if (
                new BL.ServiceGeral().Gravar<Modelo.Motorista>(
                    new Modelo.Motorista(collection["Nome"].ToString(),
                    collection["Cnh"].ToString(),
                    Convert.ToInt32(collection["Transportadora"].ToString()))
                    )
                )
            {
                return RedirectToAction("Index");
            }
            else 
            {
                return View();
            }
        }
        
        //
        // GET: /Motorista/Edit/5
 
        public ActionResult Edit(int id)
        {
            return View();
        }

        //
        // POST: /Motorista/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            if (
                 new BL.ServiceGeral().Alterar<Modelo.Motorista>(
                     new Modelo.Motorista(collection["Nome"].ToString(),
                     collection["Cnh"].ToString(),
                     Convert.ToInt32(collection["Transportadora"].ToString()), id)
                     )
                 )
            {
                return RedirectToAction("Index");
            }
            else
            {
                return View();
            }
        }

        //
        // GET: /Motorista/Delete/5
 
        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /Motorista/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                new BL.ServiceGeral().Excluir<Modelo.Motorista>(id);
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
        public void addview()
        {
            ViewData["Transportadora"] = new ServiceGeral().RetornarTodos<Modelo.Transportadora>();
           
        }
    }
}
